package com.wuyou.web.redis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wuyou.web.redis.config.propertise.BaseRedisProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by glinlf's IDEA
 *
 * @Author glinlf
 * @DATE 2018/1/24 14:52
 * Description: redis 多数据源  配置多个redisTemplate
 */

//@Configuration
public abstract class RedisConfig {


    /**
     * 配置基本连接
     * @param baseRedisProperties
     * @return
     */
    public RedisConnectionFactory redisConnectionFactory(BaseRedisProperties baseRedisProperties) {

        JedisConnectionFactory jedis = new JedisConnectionFactory();

        jedis.setHostName(baseRedisProperties.getHostName());
        jedis.setPort(baseRedisProperties.getPort());
        jedis.setPassword(baseRedisProperties.getPassword());
        jedis.setDatabase(baseRedisProperties.getDatabase());
        jedis.setTimeout(baseRedisProperties.getTimeOut());

        // 连接池设置
        jedis.setPoolConfig(jedisPoolConfig(baseRedisProperties));

        // 初始化连接
        jedis.afterPropertiesSet();
        RedisConnectionFactory factory = jedis;

        return factory;

    }

    /**
     * 配置连接池
     * @param baseRedisProperties
     * @return
     */
    private JedisPoolConfig jedisPoolConfig(BaseRedisProperties baseRedisProperties) {

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(baseRedisProperties.getMaxIdle());
        poolConfig.setMaxTotal(baseRedisProperties.getMaxTotal());
        poolConfig.setMaxWaitMillis(baseRedisProperties.getMaxWaitMillis());
        poolConfig.setTestOnBorrow(baseRedisProperties.getTestOnBorrow());
        return poolConfig;
    }

    /**
     * 初始化redis模板
     * 自定义序列化方式
     *
     * @param redisTemplate
     */
    public void initDomainTemplate(RedisTemplate<String, String> redisTemplate) {

        //定义value的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        // 初始化连接pool
        redisTemplate.afterPropertiesSet();

    }


}
